package app.cms.machining;

import java.sql.Connection;

import org.limp.basework.MachiningException;
import org.limp.basework.SimpleBean;
import org.limp.basework.SimpleBean4Table;
import org.limp.basework.SimpleBeanMachining;
import org.limp.basework.Table;
import org.limp.basework.impl.CommonBean4HSQ;
import org.limp.basework.impl.SimpleBean4TableImpl;
import org.limp.basework.tools.BaseworkUtil;
import org.limp.mine.DataTrimmerI;
import org.limp.mine.StringTool;

public class TravelGuideMachining implements SimpleBeanMachining {

	public SimpleBean machining(SimpleBean bean, Table table,
			CommonBean4HSQ cbh, Connection conn, int type)
			throws MachiningException {
		// TODO Auto-generated method stub
		if (type == 0) {
			String pkId = StringTool.checkString(bean.getResource()
					.get("PK_ID"));

			String poiIds = StringTool.checkString(bean.getResource().get(
					"T_POIID"));

			String[] poiIdstring = poiIds.split(",");

			if (poiIdstring.length > 0) {
				BaseworkUtil handler = new BaseworkUtil();
				DataTrimmerI dti = new DataTrimmerI(conn);
				SimpleBean4Table poiBean = new SimpleBean4TableImpl(
						"cmstravelguidepoi");

				for (int i = 0; i < poiIdstring.length; i++) {
					String poiId = poiIdstring[i];

					if (dti
							.haveMoreData(
									"SELECT TG_ID FROM APP_CMS_TRAVELGUIDEPOI WHERE TG_ID = ? AND POI_ID = ?",
									pkId, poiId)) {

						poiBean.getResource().put("TG_ID", pkId);
						poiBean.getResource().put("POI_ID", poiId);

						try {
							handler.manualSave1(poiBean, conn);
						} catch (Exception ex) {
							throw new MachiningException(
									"insert cmstravelguidepoi error.", ex);
						}
					}

				}
			}
		} else if (type == 1) {
			String pkId = StringTool.checkString(bean.getResource()
					.get("PK_ID"));

			DataTrimmerI dti = new DataTrimmerI(conn);
			dti.execute("DELETE FROM APP_CMS_TRAVELGUIDEPOI WHERE TG_ID = ?",
					pkId);

			String poiIds = StringTool.checkString(bean.getResource().get(
					"T_POIID"));

			String[] poiIdstring = poiIds.split(",");

			if (poiIdstring.length > 0) {
				BaseworkUtil handler = new BaseworkUtil();

				SimpleBean4Table poiBean = new SimpleBean4TableImpl(
						"cmstravelguidepoi");

				for (int i = 0; i < poiIdstring.length; i++) {
					String poiId = poiIdstring[i];

					if (!dti
							.haveMoreData(
									"SELECT TG_ID FROM APP_CMS_TRAVELGUIDEPOI WHERE TG_ID = ? AND POI_ID = ?",
									pkId, poiId)) {

						poiBean.getResource().put("TG_ID", pkId);
						poiBean.getResource().put("POI_ID", poiId);

						try {
							handler.manualSave1(poiBean, conn);
						} catch (Exception ex) {
							throw new MachiningException(
									"update cmstravelguidepoi error.", ex);
						}
					}

				}
			}
		} else if (type == 2) {
			String pkId = StringTool.checkString(bean.getResource()
					.get("PK_ID"));
			DataTrimmerI dti = new DataTrimmerI(conn);
			dti.execute("DELETE FROM APP_CMS_TRAVELGUIDEPOI WHERE TG_ID = ?",
					pkId);
		}
		return null;
	}

}
